import { SandService } from '@/core/sand.service';
import { CurrentUser, Public, RealIp } from '@/decorators';
import { halt, ok } from '@/utils';
import { Controller, Get, Post } from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('sand')
export class SandController extends BaseController {
  constructor(private readonly sandService: SandService) {
    super();
  }

  @Post('wallet')
  async wallet(@CurrentUser('id') userId: number, @RealIp() ip: string) {
    const user = await this.prisma.user.findUnique({ where: { id: userId } });
    if (!user) {
      return halt('用户信息异常');
    }
    if (!user.isReal) {
      return halt('请先完成实名认证');
    }
    if (!user.sandId) {
      return halt('杉徳钱包异常');
    }

    const res = await this.sandService.wallet({
      orderNo: await this.sandService.generateOrderNo(),
      orderAmt: '0.5',
      ip,
      notifyUrl: this.sandService.NotifyUrl,
      returnUrl: this.sandService.ReturnUrl,
      goodsName: `云账户`,
      extra: JSON.stringify({
        userId: user.sandId,
        nickName: user.realname,
        accountType: '1',
      }),
    });

    return ok(res);
  }

  @Public()
  @Get('test')
  async test(@RealIp() ip: string) {
    // return await this.sandService.wallet({
    //   orderNo: await this.sandService.generateOrderNo(),
    //   orderAmt: '0.5',
    //   ip,
    //   notifyUrl: this.sandService.NotifyUrl,
    //   returnUrl: this.sandService.ReturnUrl,
    //   goodsName: `云账户`,
    //   extra: JSON.stringify({
    //     userId: '666666',
    //     nickName: '张三',
    //     accountType: '1',
    //   }),
    // });
    return ok(await this.sandService.queryC2C('2024051114402725428021865'));
  }
}
